package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.tools.DateUtil;


/**
 * 家庭档案管理控制
 * @author 沉羽墨玖
 *
 */
public class FamilyServlet extends BaseServlet{
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m=request.getParameter("m");
		
		try{
			
	    if("list".equals(m)){
	      process(request, response, "/page/family/family_list.jsp");
	    }else if("input".equals(m)){
		  process(request, response, "/page/family/family_add.jsp");
		}else if("get".equals(m)){
			  process(request, response, "/page/family/family_edit.jsp");
		}else if ("member".equals(m)) {
			process(request, response, "/page/family/family_addMember.jsp");
		}else if("add".equals(m)){
			add(request, response);
			process(request, response, "/page/family/family_list.jsp");
		}else if("edit".equals(m)){
			edit(request, response);
		}else if("addMember".equals(m)){
			addMember(request,response);
		}else if("del".equals(m)){
			del(request, response);
		}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
private void edit(HttpServletRequest req,HttpServletResponse resp)throws Exception{
		 //1:获取档案信息
	     String famCode=req.getParameter("famCode");
	     String persCode=req.getParameter("persCode");
	     String creatTime = req.getParameter("creatTime");
		 String townNo=req.getParameter("town"); 
		 String villageNo=req.getParameter("village");  
		 String groupNo=req.getParameter("group");
		 String housePro=req.getParameter("housePro");
		 String address=req.getParameter("address");
		 //从session对象中获取当前用户名称
		 String creator=(String)req.getSession().getAttribute("userName");
		 //1.1实例化新家庭档案对象
		 Family family =new Family(famCode);
		 //将请求参数依次赋值给实例的成员属性
		 Area town=new Area();
		 town.setAreacode(townNo);
		 Area village=new Area();
		 village.setAreacode(villageNo);
		 Area group=new Area();
		 group.setAreacode(groupNo);
		 
		 family.setTown(town);
		 family.setVillage(village);
		 family.setGroupNo(group);
		 family.setHousePro(housePro);
		 family.setCreator(creator);
		 family.setAddress(address);
		 //2:获取户主个人信息
		 String cardId=req.getParameter("cardId");
		 String persName=req.getParameter("persName");		 
		 String sex=req.getParameter("sex");
		 String birthday=req.getParameter("birthday");
		 String persPro=req.getParameter("persPro");
		 String isRural=req.getParameter("isRural");
		 String occupation=req.getParameter("occupation");
		 String workUnit=req.getParameter("workUnit");
		 String liveAddress=req.getParameter("liveAddress");
		 String telephone=req.getParameter("telephone");
		 
		 //2.1实例化户主个人档案对象
		 Person holder=new Person(persCode);
	
		 holder.setFamily(family);
		 //将户主农民档案信息依次赋值给户主实例。
		 holder.setCardId(cardId);
		 holder.setPersName(persName);
		 holder.setSex(sex);	 
		 holder.setBirthday(birthday);
		 //调用日期组件计算年龄
		 holder.setAge(DateUtil.diffYear(birthday));
		 holder.setPersPro(persPro);
		 holder.setRural("1".equals(isRural)?true:false);
		 holder.setOccupation(occupation);
		 holder.setWorkUnit(workUnit);
		 holder.setLiveAddress(liveAddress);
		 holder.setTelephone(telephone);
		 holder.setRelation("1");//户主
		 family.setHolder(holder);
		 //保存家庭档案信息
		
		  
		 try{
				family.edit();
				process(req, resp, "/page/family/family_list.jsp");
		}catch(Exception e){
			e.printStackTrace();
		  }
		
	}	
	
	private String add(HttpServletRequest request,HttpServletResponse response)throws Exception{
		 //1:获取档案信息
		 String townNo=request.getParameter("town");
		 String villageNo=request.getParameter("village");
		 String groupNo=request.getParameter("group");
		 String address=request.getParameter("address");
		 String housePro=request.getParameter("housePro");
		//从session对象中获取当前用户名称
		 String creator=(String)request.getSession().getAttribute("userName");
		 //1.1实例化新家庭档案对象
		 Family family =new Family();
		//将请求参数依次赋值给实例的成员属性
		 Area town=new Area();
		 town.setAreacode(townNo);
		 Area village=new Area();
		 village.setAreacode(villageNo);
		 Area group=new Area();
		 group.setAreacode(groupNo);

		 
		 family.setTown(town);
		 family.setVillage(village);
		 family.setGroupNo(group);
		 family.setHousePro(housePro);
		 family.setAddress(address);
		 family.setCreator(creator);
		 //2:获取户主个人信息
		 String cardId=request.getParameter("cardId");
		 String persName=request.getParameter("persName");
		 String sex=request.getParameter("sex");
		 String birthday=request.getParameter("birthday");
		 String persPro=request.getParameter("persPro");
		 String isRural=request.getParameter("isRural");
		 String occupation=request.getParameter("occupation");
		 String workUnit=request.getParameter("workUnit");
		 String liveAddress=request.getParameter("liveAddress");
		 String telephone=request.getParameter("telephone");
		 
		 //2.1实例化户主个人档案对象
		 Person holder=new Person();
		 //灏嗘埛涓诲啘姘戞。妗堜俊鎭緷娆¤祴鍊肩粰鎴蜂富瀹炰緥銆�
		 holder.setCardId(cardId);
		 holder.setPersName(persName);
		 holder.setSex(sex);	 
		 holder.setBirthday(birthday);
		 System.out.println("++++birthday++++"+birthday);
		//调用日期组件计算年龄
		 holder.setAge(DateUtil.diffYear(birthday));
		 System.out.println("--===-setAge-=="+DateUtil.diffYear(birthday));
		 
		 holder.setPersPro(persPro);
		 holder.setRural("1".equals(isRural)?true:false);
		 holder.setOccupation(occupation);
		 holder.setWorkUnit(workUnit);
		 holder.setLiveAddress(liveAddress);
		 holder.setTelephone(telephone);
		 holder.setRelation("1");//鎴蜂富
		 family.setHolder(holder);
		//保存家庭档案信息
		 family.addFamily();
		 return "/page/family/family_list.jsp";
		
	}	
	private void addMember(HttpServletRequest request,HttpServletResponse response)throws Exception{
		 //1:鑾峰彇妗ｆ淇℃伅
		    String famCode=request.getParameter("famCode");
		 //浠巗ession瀵硅薄涓幏鍙栧綋鍓嶇敤鎴峰悕绉�
		 String creator=(String)request.getSession().getAttribute("userName");
		 //1.1瀹炰緥鍖栨柊瀹跺涵妗ｆ瀵硅薄
		 Family family =new Family();
		 family.setFamCode(famCode);
		 //2:鑾峰彇鎴蜂富涓汉淇℃伅
		 String cardId=request.getParameter("cardId");
		 String persName=request.getParameter("persName");
		 String sex=request.getParameter("sex");
		 String birthday=request.getParameter("birthday");
		 String persPro=request.getParameter("persPro");
		 String isRural=request.getParameter("isRural");
		 String occupation=request.getParameter("occupation");
		 String workUnit=request.getParameter("workUnit");
		 String liveAddress=request.getParameter("liveAddress");
		 String telephone=request.getParameter("telephone");
		 String relation =request.getParameter("relation");
		 
		 
		 //2.1瀹炰緥鍖栨埛涓讳釜浜烘。妗堝璞�
		 Person member=new Person();
		 member.setCardId(cardId);
		 member.setPersName(persName);
		 member.setSex(sex);	 
		 member.setBirthday(birthday);
		 //璋冪敤鏃ユ湡缁勪欢璁＄畻骞撮緞
		 member.setAge(DateUtil.diffYear(birthday));
		 member.setPersPro(persPro);
		 member.setRural("1".equals(isRural)?true:false);
		 member.setOccupation(occupation);
		 member.setWorkUnit(workUnit);
		 member.setLiveAddress(liveAddress);
		 member.setTelephone(telephone);
		 member.setRelation(relation);
		 member.setFamily(family);
		 int agriNum=member.isRural()?1:0;
		 //淇濆瓨瀹跺涵鎴愬憳淇℃伅
		 member.addMember(agriNum);
		 process(request, response, "/page/family/family_list.jsp");
		
	}
	
	private void del(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

	  	  String famCode=request.getParameter("famCode");
	  	  Family family=new Family();
	  	  try{
	  		System.out.println(famCode);
	  		family.del(famCode);
	  		process(request, response, "/page/family/family_list.jsp");
	  	  }catch(Exception e){
	  		e.printStackTrace();
	  	  }
	  
		}

}
